class Solution
{
    vector<string> ret;
    string path;
    vector<string> hash = { "", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz" };
public:
    vector<string> letterCombinations(string digits)
    {
        if (digits == "") return ret;
        dfs(digits, 0);
        return ret;
    }
    void dfs(string& digits, int pos)
    {
        if (path.size() == digits.size())
        {
            ret.push_back(path);
            return;
        }
        for (auto ch : hash[digits[pos] - '0'])
        {
            path.push_back(ch);
            dfs(digits, pos + 1);
            path.pop_back();
        }
    }
};
class Solution
{
    string path;
    vector<string> ret;
    int left = 0, right = 0;
    int n;
public:
    vector<string> generateParenthesis(int _n)
    {
        n = _n;
        dfs();
        return ret;
    }
    void dfs()
    {
        if (right == n)
        {
            ret.push_back(path);
            return;
        }
        if (left < n)
        {
            path.push_back('(');
            left++;
            dfs();
            path.pop_back();
            left--;
        }
        if (left > right)
        {
            path.push_back(')');
            right++;
            dfs();
            path.pop_back();
            right--;
        }
    }
};